<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="dept" default-expand-all :expand-on-click-node="false">
        <!-- data: 当前节点的数据 -->
        <template #default="{data}">
          <!-- 自定义树形组件一行的结构 -->
          <el-row type="flex" style="width:100%;height:40px;" justify="space-between" align="middle">
            <el-col :span="20">{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!--
                下拉菜单的展示: 点击时展示
                trigger="click"
               -->
              <el-dropdown trigger="click" @command="oprateDept($event, data.id)">
                <span>
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>

      <!-- 添加部门对话框  -->
      <!--
        ref:
        给DOM标签绑定ref, 就可以获取到冬梅标签
        给组件绑定ref, 就可以获取到组件, 从而调用组件中的方法
       -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :current-node-id="currentNodeId" @updateDepartment="getDepartment" />
    </div>
  </div>
</template>
<script>
import { reqGetDepartment, reqDelDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './component/AddDept.vue'

import hello from '@/mixins/hello'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  mixins: [hello],
  data() {
    return {
      dept: [],
      // 控制添加对话框的显示/隐藏
      showDialog: false,
      // 当前操作部门的id
      currentNodeId: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 1. 获取部门列表数据
    async getDepartment() {
      const res = await reqGetDepartment()
      this.dept = transListToTreeData(res, 0)
    },
    // 2. 处理右侧下拉菜单的点击
    oprateDept(type, id) {
      console.log(type, id)
      // 2.1 添加操作
      if (type === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      }
      // 2.2 编辑操作
      if (type === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        this.$refs.addDept.getDeptDetail(id)
      }
      // 2.3 删除操作
      if (type === 'del') {
        this.$confirm('确定删除该部门吗?').then(async() => {
          // 2.3.1 调用删除部门接口函数
          await reqDelDepartment(id)
          // 2.3.2 提示用户
          this.$message.success('删除部门成功')
          // 2.3.3 重新拉取数据
          this.getDepartment()
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{
  padding: 30px 140px;
  font-size: 14px;
}

.tree-manager{
  width: 50px;
  display: inline-block;
  margin: 10px
}
</style>
